package unclassified.simple;

import java.util.Arrays;

public class TimeNeededToBuyTickets_5926 {

    public int timeRequiredToBuy(int[] tickets, int k) {
        int max = Arrays.stream(tickets).max().getAsInt();
        int res = 0;

        for (int i = 0; i < max; i++) {
            int second = 0;
            for (int j = 0; j < tickets.length; j++) {
                if (tickets[j] == 0) {
                    continue;
                }
                tickets[j]--;
                second++;
                if (tickets[k] == 0) {
                    break;
                }
            }
            res += second;
            if (tickets[k] == 0) {
                break;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        TimeNeededToBuyTickets_5926 tn = new TimeNeededToBuyTickets_5926();
        System.out.println(tn.timeRequiredToBuy(new int[]{84,49,5,24,70,77,87,8}, 3));
    }
}
